﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Practices.Prism.Regions;
using HubModule.Controllers;
using HubModule.Services;
using HubModule.ViewModels;
using TimeSplicerModule.Controllers;
using TimeSplicerModule.Services;
using TimeSplicerModule.DomainModels;
using Infrastucture;

namespace HubModule.Views
{
    /// <summary>
    /// Interaction logic for HubView.xaml
    /// </summary>
    public partial class HubView : UserControl, INavigationAware
    {
        // going to add some temporary items on the page until i have time for better solution.

        private readonly IProjectController _projectController;
        private HubViewModel Model;
        private readonly IHubService _hubService;
        private readonly ITaskController _taskController;
        private readonly ITaskService _taskService;
        private readonly IHubController _hubController;

        public HubView(IProjectController projectController, IHubService hubService, ITaskController taskController, ITaskService taskService, IHubController hubController)
        {
            InitializeComponent();

            _projectController = projectController;
            _hubService = hubService;
            _taskController = taskController;
            _taskService = taskService;
            _hubController = hubController;
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return false; //throw new NotImplementedException();
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            //throw new NotImplementedException();\
            
        }

        public async void OnNavigatedTo(NavigationContext navigationContext)
        {
            this.itemsGrid.Visibility = System.Windows.Visibility.Hidden;
            this.progressBorder.Visibility = System.Windows.Visibility.Visible;
            this.progressRing.IsActive = true;

            await Task.Run(() => Model = _hubService.GetViewModel()).ContinueWith(_ => this.DataContext = Model, TaskScheduler.FromCurrentSynchronizationContext());

            // need to set color from status
            if (Model.InProgressTask != null)
                Model.InProgressTask.Status.SetBrushColor();

            if (Model.SuspendedTask1 != null)
                Model.SuspendedTask1.Status.SetBrushColor();

            if (Model.SuspendedTask2 != null)
                Model.SuspendedTask2.Status.SetBrushColor();

            if (Model.SuspendedTask3 != null)
                Model.SuspendedTask3.Status.SetBrushColor();

            if (Model.NotStartedTask1 != null)
                Model.NotStartedTask1.Status.SetBrushColor();

            if (Model.NotStartedTask2 != null)
                Model.NotStartedTask2.Status.SetBrushColor();

            if (Model.NotStartedTask3 != null)
                Model.NotStartedTask3.Status.SetBrushColor();

            this.itemsGrid.Visibility = System.Windows.Visibility.Visible;
            this.progressBorder.Visibility = System.Windows.Visibility.Hidden;
            this.progressRing.IsActive = false;;


           // this.DataContext = Model;
        }

        private void myProjectsTile_Click(object sender, RoutedEventArgs e)
        {
            // so instead need to send message to navigation module though app module
            _projectController.Navigate(new Infrastucture.Navigation.NavigationLink
                {
                    RegionName = RegionNames.TopNavigationRegion,
                    NavigationMenu = NavigationMenu.Projects
                });
           // _projectController.NavigateToListView();
            
        }

        private void myTasksTile_Click(object sender, RoutedEventArgs e)
        {
            _projectController.Navigate(new Infrastucture.Navigation.NavigationLink
            {
                RegionName = RegionNames.TopNavigationRegion,
                NavigationMenu = NavigationMenu.Tasks
            });

            // TODO: need to naviate to the project list view
            //_taskController.NavigateToListView();

            // I need to send a notification up to the 
            
        }

        private void myInprogressTaskTile_Click(object sender, RoutedEventArgs e)
        {
            _taskController.NavigateToItemView(Model.InProgressTask.TaskGuid);
        }

        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            Button b = sender as Button;

            if ((string)b.Content == "Open")
            {
                var task = b.DataContext as TaskModel;
                _taskController.NavigateToItemView(task.TaskGuid);
            }
            else
            {

                MessageBoxResult result = MessageBox.Show("Are you sure you want to " + b.Content + " this task?", "", MessageBoxButton.YesNo);

                if (result == MessageBoxResult.Yes)
                {
                    this.itemsGrid.Visibility = System.Windows.Visibility.Hidden;
                    this.progressBorder.Visibility = System.Windows.Visibility.Visible;
                    this.progressRing.IsActive = true;

                    var task = b.DataContext as TaskModel;
                    if ((string)b.Content == "Activate")
                    {
                        await Task.Run(() => _taskService.Activate(ModelConverter.ToModel<TimeSplicerModule.ViewModels.TaskViewModel>(task)));
                    }
                    else if ((string)b.Content == "Suspend")
                    {
                        await Task.Run(() => _taskService.Suspend(ModelConverter.ToModel<TimeSplicerModule.ViewModels.TaskViewModel>(task)));
                    }
                    else if ((string)b.Content == "Complete")
                    {
                        await Task.Run(() => _taskService.Complete(ModelConverter.ToModel<TimeSplicerModule.ViewModels.TaskViewModel>(task)));
                    }

                    // TODO: Refresh the page
                    _hubController.NavigateToView();
                }
            }

        }
    }
}
